//
//  SplittedDocumentViewController.m
//  Documents
//
//  Created by Alex Di Mango 
//  Copyright (c) 2012 Whardoo.com -  All rights reserved.
//

#import "SplittedDocumentViewController.h"
#import "ReaderMainToolbar.h"
#import "ReaderThumbQueue.h"
#import "ReaderThumbCache.h"

@implementation SplittedDocumentViewController

@synthesize firstView, firstDocument, secondView, secondDocument, firstTitle, secondTitle, toolbar;
@synthesize firstReader,secondReader;
@synthesize firstReaderViewController,secondReaderViewController;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.firstReader = [ReaderDocument withDocumentFilePath:[self.firstDocument getDocumentPath] localFileName:[[self.firstDocument getFileId] stringValue] fileType:[self.firstDocument getFileType] password:nil document:self.firstDocument];    


    self.secondReader = [ReaderDocument withDocumentFilePath:[self.secondDocument getDocumentPath] localFileName:[[self.secondDocument getFileId] stringValue]fileType:[self.secondDocument getFileType] password:nil document:self.secondDocument];

        
    //First reader viewController
    ReaderViewController *_firstReaderViewController = [[ReaderViewController alloc] initWithReaderDocument:firstReader];
    self.firstReaderViewController = _firstReaderViewController;
    [_firstReaderViewController release];
    self.firstReaderViewController.comparing = YES;
    self.firstReaderViewController.delegate = self;
    self.firstReaderViewController.showInMyDocuments = YES;
    self.firstReaderViewController.view.frame = CGRectMake(0, 0, 511, 704);
    
    //Second reader viewController
    ReaderViewController *_secondReaderViewController = [[ReaderViewController alloc] initWithReaderDocument:secondReader];
    self.secondReaderViewController = _secondReaderViewController;
    [_secondReaderViewController release];
    self.secondReaderViewController.comparing = YES;
    self.secondReaderViewController.delegate = self;
    self.secondReaderViewController.showInMyDocuments = YES;
    self.secondReaderViewController.view.frame = CGRectMake(0, 0, 511, 704);

    
    [self.firstView addSubview: self.firstReaderViewController.view];
    [self.secondView addSubview: self.secondReaderViewController.view ];
    
    self.firstTitle.title = [NSString stringWithFormat:@"%@/%@", [self.firstDocument getFolderName], [self.firstDocument getDocumentTitle]];
    self.secondTitle.title = [NSString stringWithFormat:@"%@/%@", [self.secondDocument getFolderName], [self.secondDocument getDocumentTitle]];
}


- (void)viewDidUnload
{
        
    [super viewDidUnload];
    
    self.firstDocument = nil;
    self.firstView = nil;
    self.firstReaderViewController = nil;
    self.secondReaderViewController = nil;
    self.firstReader = nil;
    self.secondReader = nil;
    self.secondDocument = nil;
    self.secondView = nil;
    self.firstTitle = nil;
    self.secondTitle = nil;
    self.toolbar = nil;
}

-(void)dealloc
{
    [[ReaderThumbQueue sharedInstance] cancelOperationsWithGUID:firstReader.guid];
    [[ReaderThumbQueue sharedInstance] cancelOperationsWithGUID:secondReader.guid];

	[[ReaderThumbCache sharedInstance] removeAllObjects]; // Empty the thumb cache
    
    [firstReaderViewController release];
    [secondReaderViewController release];
    
    [firstReader release];
    [secondReader release];
    [firstTitle release];
    [secondTitle release];
    [firstDocument release];
    [secondDocument release];
    [firstView release];
    [secondView release];
    [toolbar release];
    
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(UIInterfaceOrientationIsPortrait(interfaceOrientation))
    {
        return NO;
    }
    return YES;
}

-(IBAction)doneButton:(id)sender
{
    [[self presentingViewController] dismissModalViewControllerAnimated:YES];
}

-(IBAction)toggleToolbar:(id)sender
{
    if (toolbar.hidden)
	{
		[UIView animateWithDuration:0.25 delay:0.0
                            options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                         animations:^(void)
         {
             toolbar.hidden = NO;
             toolbar.alpha = 1.0f;
         }
                         completion:NULL
         ];
	}
    else
    {
        [UIView animateWithDuration:0.25 delay:0.0
                            options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                         animations:^(void)
         {
             toolbar.alpha = 0.0f;
         }
                         completion:^(BOOL finished)
         {
             toolbar.hidden = YES;
         }
         ];
    }
}

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
    [self toggleToolbar:nil];
}

@end
